#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


class Solution {
public:
    static bool Less(const vector<int>& a, const vector<int>& b) {
        return a[0] < b[0];
    }

    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end(), Less);
        vector<vector<int>> res;
        vector<int> curr{intervals[0]};
        for (int i=1; i<intervals.size(); ++i) {
            vector<int>& interval = intervals[i];
            if (interval[0] <= curr[1]) {
                curr[1] = max(curr[1], interval[1]);
            } else {
                res.push_back(curr);
                curr = {interval[0], interval[1]};
            }
        }
        res.push_back(curr);
        return res;
    }
};


int main() {
    Solution soln;
    vector<vector<int>> intervals1 = {{2,6},{1,3},{8,10},{15,18}};
    vector<vector<int>> intervals2 = {{1,4},{4,5}};
    vector<vector<int>> result = soln.merge(intervals1);
    return 0;
}
